package thread.synchroCode;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

@Slf4j(topic = "123412341234")
public class _12SelfSemaphoreTest {
  public static void main(String[] args) {
    Semaphore semaphore1 = new Semaphore(1);
    Semaphore semaphore2 = new Semaphore(0);

    new Thread(()->{
      for (int i = 0; i < 5; i++) {
          try {
            semaphore1.acquire();
            log.debug("A");
          } catch (InterruptedException e) {
            e.printStackTrace();
          } finally {
            semaphore2.release();
          }
      }
    }).start();

    new Thread(()->{
      for (int i = 0; i < 5; i++) {
        try {
          semaphore2.acquire();
          log.debug("B");
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          semaphore1.release();
        }
      }
    }).start();
  }
}
